//package com.tianji.remark.service.impl;
//
//import com.tianji.api.dto.msg.LikedTimesDTO;
//import com.tianji.api.dto.trade.OrderBasicDTO;
//import com.tianji.common.autoconfigure.mq.RabbitMqHelper;
//import com.tianji.common.constants.MqConstants;
//import com.tianji.common.utils.StringUtils;
//import com.tianji.common.utils.UserContext;
//import com.tianji.remark.domain.dto.LikeRecordFormDTO;
//import com.tianji.remark.domain.po.LikedRecord;
//import com.tianji.remark.mapper.LikedRecordMapper;
//import com.tianji.remark.service.ILikedRecordService;
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import lombok.RequiredArgsConstructor;
//import org.springframework.stereotype.Service;
//
//import java.util.List;
//import java.util.Set;
//import java.util.stream.Collectors;
//
//*
// * <p>
// * 点赞记录表 服务实现类
// * </p>
// *
// * @author 晚风
// * @since 2023-09-24
//
//
//@Service
//@RequiredArgsConstructor
//public class LikedRecordServiceImpl extends ServiceImpl<LikedRecordMapper, LikedRecord> implements ILikedRecordService {
//
//    private final RabbitMqHelper rabbitMqHelper;
//
//    @Override
//    public void addLikeRecord(LikeRecordFormDTO dto) {
//        // 1. 获取当前用户id
//        Long userId = UserContext.getUser();
//        // 2. 判断是否点赞 liked为true表示点赞
////        boolean flag = true;
////        if (dto.getLiked()){
////            // 2.1. 点赞逻辑
////            flag = liked(dto);
////        }else {
////            // 2.2. 取消点赞
////            flag = unliked(dto);
////        }
//        boolean flag = dto.getLiked() ? liked(dto,userId) : unliked(dto,userId);
//        if (!flag){
//            return;
//        }
//        // 3. 根据业务id统计点赞数量
//        Integer totalLikedNum = this.lambdaQuery().eq(LikedRecord::getBizId, dto.getBizId()).count();
//        // 4. 使用MQ发送消息
//        String routingKey = StringUtils.format(MqConstants.Key.LIKED_TIMES_KEY_TEMPLATE, dto.getBizType());
//        LikedTimesDTO msg = new LikedTimesDTO();
//        msg.setLikedTimes(totalLikedNum);
//        msg.setBizId(dto.getBizId());
//        rabbitMqHelper.send(
//                MqConstants.Exchange.LIKE_RECORD_EXCHANGE,
//                routingKey,
//                msg
//                );
//    }
//
//    @Override
//    public Set<Long> getLikesStatusByBizIds(List<Long> bizIds) {
//        // 1. 获取用户id
//        Long userId = UserContext.getUser();
//        // 2. 查询点赞记录表
//        List<LikedRecord> likedRecords = this.lambdaQuery().in(LikedRecord::getBizId, bizIds).eq(LikedRecord::getUserId, userId).list();
//        Set<Long> likedBizIds = likedRecords.stream().map(LikedRecord::getBizId).collect(Collectors.toSet());
//        // 3. 将查询到的数据返回
//        return likedBizIds;
//    }
//
//    // 取消赞
//    private boolean unliked(LikeRecordFormDTO dto, Long userId) {
//        LikedRecord record = this.lambdaQuery().eq(LikedRecord::getUserId, userId)
//                .eq(LikedRecord::getBizId, dto.getBizId())
//                .one();
//        if (record == null){
//            // 说明没有点过赞，取消失败
//            return false;
//        }
//        // 删除点赞数量
//        boolean result = this.removeById(record.getId());
//        return result;
//    }
//    // 点赞
//    private boolean liked(LikeRecordFormDTO dto, Long userId) {
//        LikedRecord record = this.lambdaQuery().eq(LikedRecord::getUserId, userId)
//                .eq(LikedRecord::getBizId, dto.getBizId())
//                .one();
//        if (record != null){
//            // 说明已经点过赞
//            return false;
//        }
//        LikedRecord likesRecord = new LikedRecord();
//        likesRecord.setUserId(userId);
//        likesRecord.setBizId(dto.getBizId());
//        likesRecord.setBizType(dto.getBizType());
//        boolean result = this.save(likesRecord);
//        return result;
//    }
//}
